<?php
namespace Models\Model;
use Zend\Db\ResultSet\ResultSet;
use Zend\Db\TableGateway\TableGateway;
use Zend\Db\Sql;
use Zend\Db\Sql\Where;
use Zend\Db\Sql\Select;
use Zend\Paginator\Adapter\DbSelect;
use Zend\Paginator\Paginator;
use Zend\Db\Sql\Predicate;
use Zend\Db\Sql\Expression;
class OrderDeliveryDetailsTable
{
    protected $tableGateway;
	protected $select;
    public function __construct(TableGateway $tableGateway)
    {
        $this->tableGateway = $tableGateway;
		$this->select = new Select();
    }
	public function getShopOrders($shop_id){ 
		
		$select = $this->tableGateway->getSql()->select();
		$select->join('user_medicine_order', new Expression('user_medicine_order.user_medicine_order_id=order_delivery_details.user_medicine_order_id'),array('*'),'left');
		$select->join('user', new Expression('user.user_id=user_medicine_order.user_id'),array('user_id','user_name'),'left');
		$select->join('user_details', new Expression('user_details.user_id=user.user_id'),array('phone','location','landmark','address','alterphone'),'left');		
		$select->where('order_delivery_details.shop_id="'.$shop_id.'"'); 
		$select->order('order_delivery_details.order_delivery_details_id DESC');		
		$resultSet = $this->tableGateway->selectWith($select);
		return $resultSet;
    }
		
	public function updateOrderPrice($order_delivery_details_id,$price){
	   $data = array(
		   'price'      		=>	$price,
		   'shop_order_status'  =>	1,
		   'date_updated'      	=>	date('Y-m-d H:i:s'),
		);
	   $result=$this->tableGateway->update($data, array('order_delivery_details_id' => $order_delivery_details_id));	   
	   return $result;                        
   }
   
   public function assignOrdersToShops($order)
   {
		$shopIds=explode('***',$order['shop_ids']);
		foreach($shopIds as $shopId)
		{
			$result[]=$this->saveOrder($shopId,$order['user_medicine_order_id'],$order['discount']);
		}
		return $result;
   }
   public function deletePrevious($mOrderId)
   {
		$row=$this->tableGateway->delete(array('(user_medicine_order_id IN ('.$mOrderId.'))'));
		return $row;
   }
   public function saveOrder($shopId,$orderId,$discount)
   {
		$data = array(		
			'shop_id'          					=> 	$shopId,  		
			'user_medicine_order_id'          	=> 	$orderId,		
			'discount'	          				=> 	$discount,
			'user_order_status'	          		=> 	1,
			'shop_order_status'	          		=> 	0,
			'payment_status'	          		=> 	0,
			'admin_process_status'	          	=> 	0,
			'date_updated'						=>  date('Y-m-d H:i:s')
		);	
		$insertresult=$this->tableGateway->insert($data);	
		return $this->tableGateway->lastInsertValue;
   }
   public function updateOderWithQutation($shopOrderId,$order)
   {
		$data = array(		
			'price'				=>	$order['price'],	  	
			'shop_order_status'	=>	1,
			'date_updated'		=>	date('Y-m-d H:i:s'), 	
		);	
		$updateuserid=$this->tableGateway->update($data, array('order_delivery_details_id' => $shopOrderId));
		return $updateuserid;
   }
    public function updateShopOrdersByAdmin($order,$type)
   {
		if($type=='delete'){
			$orderIds=explode('***',$order);
			foreach($orderIds as $orderId)
			{
				$data = array(		
					'admin_process_status'	=>	1,
					'shop_order_status'		=>	3,
					'date_updated'			=>	date('Y-m-d H:i:s'), 	
				);
				$updateuserid=$this->tableGateway->update($data, array('order_delivery_details_id' => $orderId));
				$result[]=$updateuserid;
			}
		}else{
			
			$data = array(		
					'admin_process_status'	=>	1,
					'shop_order_status'		=>	2,
					'date_updated'			=>	date('Y-m-d H:i:s'), 	
				);
			$updateuserid=$this->tableGateway->update($data, array('order_delivery_details_id' => $order));
			$result=$updateuserid;
		}
		return $result;
   }
    public function getOrderdetailes($o_id){	
		$select = $this->tableGateway->getSql()->select();
		$select->join('user_medicine_order', new Expression('user_medicine_order.user_medicine_order_id=order_delivery_details.user_medicine_order_id'),array('*'),'left');
		$select->where('order_delivery_details.user_medicine_order_id="'.$o_id['trackingId'].'"');
		$resultSet = $this->tableGateway->selectWith($select);	
		return $resultSet;		
	}
   public function getShopReports($shop_id){	
		$select = $this->tableGateway->getSql()->select();
		$select->join(array('user'=>'order_delivery_details'), 'order_delivery_details.order_delivery_details_id=user.order_delivery_details_id',array('counts'=>new Expression('COUNT(user.shop_order_status)')),'left');
		$select->Group('user.shop_order_status');
		$select->where('user.shop_id="'.$shop_id.'"');
		$resultSet = $this->tableGateway->selectWith($select);	
		return $resultSet->count();		
	}
	public function getShopwiseReportsforOrders($shop_id,$type){
		$select = $this->tableGateway->getSql()->select();
		$select->join('user_medicine_order', new Expression('user_medicine_order.user_medicine_order_id=order_delivery_details.user_medicine_order_id'),array('*'),'left');
		if($type=='auto_renewal'){
			$select->where('user_medicine_order.auto_renewal="1"');
		}else if($type=='cnt_of_sent_price'){
			$select->where('order_delivery_details.shop_order_status="1"');
		}else if($type=='cnt_of_cnf_by_admin'){	
			$select->where('order_delivery_details.admin_process_status="1"');
			$select->where('order_delivery_details.shop_order_status="2"');
		}else if($type=='delivered_orders'){
			$select->where('order_delivery_details.admin_process_status="1"');
			$select->where('order_delivery_details.shop_order_status="2"');
			$select->where('order_delivery_details.user_order_status="2"');
		}
		$select->where('order_delivery_details.shop_id="'.$shop_id.'"');
		// $select->Group('order_delivery_details.shop_order_status');
		$resultSet = $this->tableGateway->selectWith($select);	
		return $resultSet->count();	
	}	
	public function getTotalOrdersReports($data)
	{
		$fdate = date_create($data['from_date']);
		$from_date=date_format($fdate, 'Y-m-d');
		$tdate = date_create($data['to_date']);
		$to_date=date_format($tdate, 'Y-m-d');
		
		$select = $this->tableGateway->getSql()->select();
		$select->join('user_medicine_order', new Expression('user_medicine_order.user_medicine_order_id=order_delivery_details.user_medicine_order_id'),array('*'),'left');
		$select->join('user', new Expression('user.user_id=user_medicine_order.user_id'),array('user_id','user_name','email'),'left');
		$select->join('user_details', new Expression('user_details.user_id=user.user_id'),array('city','state','country','address'),'left');	
		$select->join(array('shop'=>'user'), new Expression('shop.user_id=order_delivery_details.shop_id'),array('shop_id'=>'user_id','shop_name'=>'user_name'),'left');
		$select->join(array('shop_details'=>'user_details'), new Expression('shop_details.user_id=shop.user_id'),array('shop_city'=>'city','shop_state'=>'state','shop_country'=>'country','shop_address'=>'address'),'left');
		/*if(isset($data['current_page']) && $data['current_page']!='')
		{
			$currentPage=$data['current_page'];
		}
		else{
		$currentPage='1';
		}
		if(isset($data['results_per_page']) && $data['results_per_page']!='')
		{
			$resultsPerPage=$data['results_per_page'];
		}
		else{
		$resultsPerPage='10';
		}
		$limit=$resultsPerPage;
		$offset=($currentPage-1)*$resultsPerPage;*/
		if((isset($from_date) && $from_date!='') && (isset($to_date) && $to_date!='')){
		$select->where('order_delivery_details.delivered_date>="'.$from_date.'"');
		$select->where('order_delivery_details.delivered_date<="'.$to_date.'"');
		}
		if($data['reports_type']=='3')
		{
			$select->where('order_delivery_details.shop_order_status="2"'); 
		}else if($data['reports_type']=='4'){
			$select->where('order_delivery_details.user_order_status="3"'); 
		}else{
			$select->where('order_delivery_details.admin_process_status="1"'); 
		}
		$select->order('order_delivery_details_id DESC');	
		$resultSet = $this->tableGateway->selectWith($select);
		return $resultSet;
	}
	public function cancelledOrder($orderId)
    {	
		$data = array(
			'user_order_status' 	=> 3, 
			'date_updated'			=> date('Y-m-d H:i:s'),
		);
		$updaed_status=$this->tableGateway->update($data, array('user_medicine_order_id' => $orderId));
		return 	$updaed_status;			
	}
	public function deleteOrders($mOrderId)
   {
		$row=$this->tableGateway->delete(array('(user_medicine_order_id IN ('.$mOrderId.'))'));
		return $row;
   }
   public function updateOrderdetailesStatus($orderId)
    {	
		$data = array(
			'user_order_status' 	=> 2, 
			'delivered_date'		=> date('Y-m-d H:i:s'),
			'date_updated'			=> date('Y-m-d H:i:s'),
		);
		$updaed_status=$this->tableGateway->update($data, array('user_medicine_order_id' => $orderId));
		return 	$updaed_status;			
	}
	public function deleteduplicateorderStatus($mOrderId)
   {
		$row=$this->tableGateway->delete(array('shop_order_status IN (3)','user_medicine_order_id IN ('.$mOrderId.')'));
		return $row;
   }
   public function selectorderdetailes($moId)
	{
		$select = $this->tableGateway->getSql()->select();
		$select->where('shop_order_status="2"');
		$select->where('user_medicine_order_id="'.$moId.'"');
		$result = $this->tableGateway->selectWith($select);	
		return $result;
	}
	public function getComplaintshopId($order_id)
	{
		$select = $this->tableGateway->getSql()->select();
		$select->where('shop_order_status="2"');
		$select->where('user_medicine_order_id="'.$order_id.'"');
		$result = $this->tableGateway->selectWith($select);	
		return $result;
	}
	public function getreplyComplaintuserId($order_id)
	{
		$select = $this->tableGateway->getSql()->select();
		$select->join('user_medicine_order', new Expression('user_medicine_order.user_medicine_order_id=order_delivery_details.user_medicine_order_id'),array('*'),'left');
		$select->where('shop_order_status="2"');
		$select->where('user_medicine_order.user_medicine_order_id="'.$order_id.'"');
		$result = $this->tableGateway->selectWith($select);	
		return $result;
	}
}